package  
{
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import org.flixel.*;
	/**
	 * ...
	 * @author morgan
	 */
	public class Human extends FlxSprite
	{
		[Embed (source = "../data/human.png")] private var human:Class;
		
		public var dying:Boolean = false;
		public var dyingTimer:Number = 0.0;
		public var dyingPoints:Array = [];
		public var walkSpeed:Number = 25;
		public var runSpeed:Number = 40;
		public var onGround:Boolean = false;
		public var onGroundTimer:Number = 0;
		public function Human(x:int, y:int) 
		{
			super(x, y);
			loadGraphic(human, true, true, 32, 32);
			addAnimation("walk", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 12, true);
			addAnimation("idle", [0]);
			play("walk");
			acceleration.y = 120;
		}
		
		public function updateDeath():void
		{
			var len:int = dyingPoints.length;
			var arr2:Array = new Array(len);
			for(var i:int = 0; i<len; i++)
			{
				arr2[i] = dyingPoints.splice(int(Math.random() * (len - i)), 1)[0];
			}			
			dyingPoints = arr2;
			
			
			var state:GameState = FlxG.state as GameState;
			
			for (i = 0; i < Math.min(9, len); ++i)
			{
				var pt:FlxPoint = dyingPoints[i];
				var col:uint = framePixels.getPixel32(pt.x, pt.y);
				if (col != 0x00000000)
				{
					framePixels.setPixel32(pt.x, pt.y, 0x00000000);
					var bug:Bug = new Bug(pt.x + x, pt.y + y, state.bugGroup);
					bug.color = col;
					bug.speed = 50;
					bug.velocity.x = Math.random() * 100 - 50;
					bug.velocity.y = Math.random() * 100 - 50;
					state.bugGroup.add(bug);
				}
				else
				{
					continue;
				}
				dyingPoints.push(new FlxPoint(pt.x - 1, pt.y));
				dyingPoints.push(new FlxPoint(pt.x + 1, pt.y));
				dyingPoints.push(new FlxPoint(pt.x, pt.y - 1));
				dyingPoints.push(new FlxPoint(pt.x, pt.y + 1));
			}
			dyingPoints.splice(0, i);
		}
		
		public function updateControls():void
		{
			var targetSpeed:Number = 0;
			if (FlxG.keys.A)
			{
				targetSpeed = -walkSpeed;
				play("walk");
				facing = LEFT;				
			}
			else if (FlxG.keys.D)
			{
				targetSpeed = walkSpeed;
				play("walk");
				facing = RIGHT;
			}
			else
			{
			//	play("idle");
				targetSpeed = 0;
			}
			
			if (FlxG.keys.SHIFT)
			{
				targetSpeed *= 2;
			}
			
			var accel:Number = onGround ? 80 : 40;
			if (velocity.x < targetSpeed)
			{
				velocity.x = Math.min(targetSpeed, velocity.x + accel * FlxG.elapsed);
			}
			if (velocity.x > targetSpeed)
			{
				velocity.x = Math.max(targetSpeed, velocity.x - accel * FlxG.elapsed);
			}
			
			if (FlxG.keys.justPressed("W") && onGround)
			{
				y -= 1;
				velocity.y = -65;
				velocity.x *= 1.2;
				onGround = false;
			}
			
			if (FlxG.keys.W && !onGround)
			{
				velocity.y -= 15 * FlxG.elapsed;
			}
			
			_curAnim.delay = 1.0 / Math.abs(velocity.x / 3);					
		}
		
		public function updateCollision():void
		{
			var mapMask:FlxSprite = (FlxG.state as GameState).map;
			
			onGroundTimer += FlxG.elapsed;			
			if (onGroundTimer > 0.15)
			{
				onGround = false;
			}
			
			if (FlxCollision.pixelPerfectCheck(this, mapMask))
			{
				var h:uint = FlxCollision.debug.height;
				var w:uint = FlxCollision.debug.width;
				
				var overlap:Rectangle = FlxCollision.debug.getColorBoundsRect(0xffffffff, 0xff00ffff, true);
				
				var leftFoot:BitmapData = new BitmapData(w/2, h/2);
				var rightFoot:BitmapData = new BitmapData(w/2, h/2);
				
				leftFoot.copyPixels(FlxCollision.debug, new Rectangle(0, h/2, w/2, h/2), new Point(0, 0), FlxCollision.debug, new Point(0, 0));
				rightFoot.copyPixels(FlxCollision.debug, new Rectangle(w / 2, h / 2, w / 2, h / 2), new Point(0, 0), FlxCollision.debug, new Point(w / 2, 0));
				
				var lrect:Rectangle = leftFoot.getColorBoundsRect(0xffffffff, 0xff00ffff, true);
				var rrect:Rectangle = rightFoot.getColorBoundsRect(0xffffffff, 0xff00ffff, true);
				
				var dl:Boolean = !lrect.isEmpty();
				var dr:Boolean = !rrect.isEmpty();		
				var hitwall:Boolean = false;
				if ( lrect.height + rrect.height >= 10)
				{
					if (lrect.height > rrect.height)
					{
						x += 1;
					}
					else
					{
						x -= 1;
					}
					//velocity.y = 0;
					hitwall = true;
					velocity.x = 0;
				}				
				
				if ((dl || dr) && velocity.y >= -30)
				{
					if (!onGround)
					{
						onGround = true;
						velocity.x *= 0.5;
					}
					onGroundTimer = 0;
					if (hitwall)
					{
						if (dl && dr)
						{
							velocity.y = 0;
						}
					}
					else
					{
						velocity.y = 0;
						if ( lrect.height + rrect.height >= 3)
						{
							y -= 1;
							
							velocity.x *= 0.8;
							if ( lrect.height + rrect.height >= 6)
							{
								velocity.x *= 0.8;
							}
						}
					}
					
				}						
			}	
		}
		
		override public function update():void 
		{
			if (dying)
			{
				play("idle");
				velocity.y = 0;
				acceleration.y = 0;
				velocity.x = 0;
				if (dyingTimer <= 0)
				{
					updateDeath();
				}
				else
				{
					dyingTimer -= FlxG.elapsed;
				}
			}
			else
			{
				updateControls()
			}
			updateCollision();			
			
			if (FlxG.keys.justPressed("K"))
			{
				var tx:int = FlxG.mouse.x - x;
				var ty:int = FlxG.mouse.y - y;
				if (framePixels.getPixel32(tx, ty) != 0x00000000)
				{
					dying = true;
					dyingTimer = 0.1;
					dyingPoints = [new FlxPoint(tx, ty)];
					FlxG.flash(0xffffffff, 0.4);
					FlxG.shake(0.005, 0.1);
				}				
			}
			
			super.update();
		}
		
	}

}